/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manager;

import JDBCUtil.MyConn;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.admin.Professional;

/**
 *
 * @author Danh
 */
public class ProManager {
    
    Connection conn;
    
    public ProManager() {
        try {
            conn = MyConn.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public List<Professional> getall() {
        List<Professional> listpro = new ArrayList<Professional>();
        try {
            
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM Professional_Types");
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Professional pr = new Professional();
                pr.setProId(rs.getInt("Professional_Id"));
                pr.setProStatus(rs.getBoolean("Professional_Status"));
                pr.setProName(rs.getNString("Professinal_Name"));
                listpro.add(pr);
            }
            return listpro;
        } catch (Exception e) {
        }
        return null;
    }
    
    public List<Professional> getone(int id) {
        List<Professional> listpro = new ArrayList<Professional>();
        try {
            
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM Professional_Types WHERE Professional_Id=?");
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Professional pr = new Professional();
                pr.setProId(rs.getInt("Professional_Id"));
                pr.setProStatus(rs.getBoolean("Professional_Status"));
                pr.setProName(rs.getNString("Professinal_Name"));
                listpro.add(pr);
            }
            return listpro;
        } catch (Exception e) {
        }
        return null;
    }
    
    public boolean updateProfessional(Professional pr) {
        PreparedStatement pstmt ;
        try {
            pstmt = conn.prepareStatement("UPDATE Professional_Types SET Professional_Status=? ,Professinal_Name =? WHERE Professional_Id=?");
            pstmt.setBoolean(1, pr.isProStatus());
            pstmt.setNString(2, pr.getProName());
            pstmt.setInt(3, pr.getProId());
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    
    public boolean insertProfesional(Professional pr) {
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement("INSERT INTO Professional_Types(Professional_Status,Professinal_Name)VALUES(?,?)");
            pstmt.setBoolean(1, pr.isProStatus());
            pstmt.setNString(2, pr.getProName());
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException ex) {
                    Logger.getLogger(ProManager.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return false;
    }
    
    public boolean deleteProfessional(int id) {
        try {
            
            PreparedStatement pstmt = conn.prepareStatement("DELETE FROM Professional_Types WHERE Professional_Id=?");
            pstmt.setInt(1, id);
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    
    public static void main(String[] args) {
        ProManager m = new ProManager();
        boolean eu = m.updateProfessional(new Professional(1, "ten day du", true));
        
    }
}
